package com.rzx.mybackend.event.mqRouteEvent.listenner;

/**
 * MessageFailureListener
 * 事件监听器
 * @author renzhexian
 * @date 2025/8/7
 */

import com.rzx.mybackend.event.mqRouteEvent.MessageFailedEvent;
import com.rzx.mybackend.service.RabbitMQService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@RequiredArgsConstructor
public class MessageFailureListener {

    private final RabbitMQService rabbitMQService;

    @EventListener
    public void handleFailedMessage(MessageFailedEvent event) {
        String messageId = event.getMessageId();
        String cause = event.getCause();

        log.info("接收到消息失败事件，ID: {}, 原因: {}", messageId, cause);

        try {
            // 调用重试方法处理失败的消息
            rabbitMQService.reytryMessage(messageId);
        } catch (Exception e) {
            log.error("重试消息失败，ID: {}", messageId, e);
        }
    }
}
